<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加试卷题目</title>
    <link rel="stylesheet" href="__PUBLIC__/BeginnerAdmin/plugins/layui/css/layui.css" media="all"/>

</head>
<body>
<div class="layui-form layui-form-pane" style="margin-left: 10px;margin-right: 10px;">
    <div class="layui-form-item" style="margin-top: 10px;">
        <label class="layui-form-label">选择题库</label>
        <div class="layui-input-block">
            <select name="city" lay-verify="examination" lay-filter="examination">

                <foreach name="ret_examination" item="vo">
                    <option value="<{$vo.examinationid}>"><{$vo.title}></option>
                </foreach>
            </select>
        </div>
    </div>
    <table id="table"></table>
    <!--下面的工具条编程-->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">题号</label>
            <div class="layui-input-inline" style="width: 200px;">
                <select name="selectQid" id="selectQid" lay-verify="">
                    <option value="">请选择一个题号</option>
                    <foreach name="ret_question_number" item="vo">
                        <option value="<{$vo.id}>"><{$vo.title_explain|mb_substr=###,0,6,'utf-8'}></option>
                    </foreach>

                </select>
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 100px;">
                <button id="TitleNumberManageButton" class="layui-btn layui-btn-primary">大题号管理</button>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 100px;">
                <button lay-submit id="select" class="layui-btn" lay-filter="select">提交</button>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript" src="__PUBLIC__/BeginnerAdmin/plugins/layui/layui.js"></script>
<script>
    function settime() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);

    }

    layui.use(['layer', 'table', 'form'], function () {
        var $ = layui.jquery, layer = layui.layer, table = layui.table, form = layui.form;

        $("#addQid").on('click', function () {
            layer.open({
                type: 1,
                title: "添加题号",
                area: ['460px', '420px'],
                content: $('#isadd'),
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    $.post('Admin_TestQuestions_NewTitleNumber.html', {
                        assignmentid: "<{$assignmentid}>"
                        , AddTitleSequence: $("#AddTitleSequence").val()
                        , TitleExplain: $("#TitleExplain").val()
                    }, function (ret) {
                        if (ret.code == 200) {
                            layer.msg('题目添加成功');
                            setTimeout('settime()', 1500);//延时执行，让人看得更清楚
                        } else {
                            layer.alert(ret.msg);
                        }
                    }, 'json');
                }
            });
        });

        form.on('submit(select)', function (data) {
            console.log(data.elem); //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form);//被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
            var checkStatus = table.checkStatus('table'); //test即为基础参数id对应的值
            var shuju = new Array();
            for (var i = 0; i < checkStatus.data.length; i++) {
                shuju[i] = {id: checkStatus.data[i].id, sequence: checkStatus.data[i].sequence};
            }


            if (checkStatus.data.length == 0) {
                layer.msg("您没有选中任何数据");
                return false;
            }

            if (data.field.selectQid == null || data.field.selectQid == "") {
                layer.msg('请选择大题');
            }

            $.post('Admin_TestQuestions_AddQID.html',
                {assignmentid: "<{$assignmentid}>", data: shuju, selectQid: data.field.selectQid},
                function (ret) {
                    if (ret.code == 200) {
                        layer.msg(ret.msg);
                    } else {
                        layer.alert(ret.msg);
                    }
                    setTimeout('settime()', 1500);
                }, 'json');

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


       $("#TitleNumberManageButton").on('click', function () {
            layer.open({
                type: 2,
                title: "管理题号",
                area: ['830px', '420px'],
                content: '<{:U('TestQuestions/TitleNumberManage',array('assignmentid'=>$assignmentid))}>',
                cancel: function (index, layero) {
                    layer.load(1);
                    window.location.reload(true);
                }
            });
        });

        var tableloding = table.render({
            elem: '#table',
            url: "<{:U('TestQuestions/SelectPoticLibrary')}>",
            where: {examinationid: '<{$ret_examination.0.examinationid}>'},
            method: 'post',
            page: true,
            cols: [[
                {checkbox: true}
                , {field: 'sequence', title: '排序', width: 80}
                , {field: 'body', title: '题目'}
                , {field: 'type', title: '类型', width: 80, templet: '#typeTpl'}
                , {field: 'knowledge', title: '知识点', width: 140}
            ]]
        });

        form.on('select(examination)', function (data) {
            tableloding.reload({
                where: {examinationid: data.value}
            });
            console.log(data);
        });

    });


</script>
<script type="text/html" id="typeTpl">
    {{#  if(d.type == 1){ }}
    单选
    {{#  } else if(d.type == 2){ }}
    多选
    {{#  } else { }}
    未定义
    {{#  } }}
</script>
</body>
</html>